<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%>
    <div id="opportunityView" title="详细信息"  style="width:800px;height:auto;padding:10px 20px;">
    
                
            <div class="tabLink" style="background:#f2f2f2;">
        		<ul>
        			<li><a href="javascript:viewOpportunityJs.openComment()">添加评论</a></li>
        			<li><a href="javascript:viewOpportunityJs.openTask()">添加任务</a></li>
        		</ul>
        	</div> 
        	<div style="margin:5px 0;clear:both"></div>
        	
        	<div class="ftitle">详细信息</div>

        	<div class="cdetail">
        		<div class="fitem">
        			<span class="view_customer12">名称:<span id="opportunity_view_name"></span></span>
        		</div>
        		<div class="fitem">
            		<span class="view_customer11">客户 :<span id="opportunity_view_customer"></span></span>
            		<span class="view_customer11">来源 :<span id="opportunity_view_from"></span></span>
            		<span class="view_customer11">当前阶段 :<span id="opportunity_view_strategy"></span></span>
            	</div>
            	<div class="fitem">
            		<span class="view_customer11">所有人 :<span id="opportunity_view_owner"></span></span>
            		<span class="view_customer11">整形项目 :<span id="opportunity_view_items"></span></span>
            		<span class="view_customer11">创建时间 :<span id="opportunity_view_times"></span></span>
            	</div>
        	</div>
        	
        	<!-- 评论列表 -->
        	<div style="margin:5px 0;clear:both"></div>
        	<div class="ftitle">评论列表</div>
            <div id="opportunityComment" class="easyui-accordion" style="width:790px;" border="false" clearStyle="true" >
        	</div>
        	
			<!-- 任务列表 -->
        	<div style="margin:5px 0;clear:both"></div>
        	<div class="ftitle">任务列表</div>
            <div id="opportunityTask" class="easyui-accordion" style="width:790px;" border="false" clearStyle="true">
        	</div>
        	
        	<!-- 咨询列表 -->
            <div style="margin:5px 0;clear:both"></div>
        	<div class="ftitle">咨询列表</div>   
        	<div title="咨询列表" style="padding:10px">
    			<table id="consultList"></table>
       	 	</div>           
    	</div>

		<div class="easyui-dialog" id="addComment" closed="true" style="width:550px;height:250px;padding:10px 20px;">
		 <form id="addComment" method="post" novalidate>
        	<div class="ftitle">评论</div>
        	<input name="comment_opportunity" type="hidden">
            <div>
                <textarea name="add_comment" style="height:100px;width:90%;margin-left:10px"></textarea>
            </div>
            <div style="text-align:center;padding:5px">
            	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="viewOpportunityJs.submitComment()">Submit</a>
        	</div>
            
        </form>
	</div>
	<div id="ConsultDetail" title="咨询详情" class="easyui-dialog"  closed="true" style="width:850px;height:450px">
    </div>
   	<div id="addTask" title="指派任务" class="easyui-dialog"  closed="true" style="width:850px;height:450px;padding:10px 20px;">
    </div>
	<script type="text/javascript">
(function( viewOpportunityJs, $, undefined ) {	
	
	var opportunity = null;
	viewOpportunityJs.init = function(){
		workbenchJs.loadData("dispatch/customer/opportunity/query?id="+getOpportunityID(),viewOpportunityJs.viewOpportunity,"查看商业机会");
		workbenchJs.loadData("dispatch/customer/opportunity/comments/query?opp="+getOpportunityID(),viewOpportunityJs.viewCustomerComments,"查看商业机会评论");
		workbenchJs.loadData("dispatch/customer/opportunity/consult/list?opp="+getOpportunityID(),viewOpportunityJs.loadConsultData,"查看咨询");
		workbenchJs.loadData("dispatch/customer/opportunity/task/query?opp="+getOpportunityID(),viewOpportunityJs.loadTaskResult,"查看商业机会任务");
	};
	function getOpportunityID(){
		return <%=request.getParameter("id")%>;
	};
	viewOpportunityJs.viewOpportunity = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var row = jQuery.parseJSON(result.payload.value);
        	opportunity = row;
        	
        	$('#opportunity_view_name').html(row.name);
    		$('#opportunity_view_customer').html(row.customerName);
    		$('#opportunity_view_from').html(row.opp_from);
    		$('#opportunity_view_strategy').html(row.strategy);
    		$('#opportunity_view_owner').html(row.owner);
    		$('#opportunity_view_items').html(row.items);
    		$('#opportunity_view_times').html(row.createTime);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	viewOpportunityJs.loadConsultData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var row = jQuery.parseJSON(result.payload.value);
        	$('#consultList').datagrid('loadData',row);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	viewOpportunityJs.loadTaskResult = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
    		var p = $('#opportunityTask').accordion('panels');
    		$(p).each(function(index){
    			
    			$('#opportunityTask').accordion('remove',0);
    		});
        	var row = jQuery.parseJSON(result.payload.value);
        	row = row.rows;
    		$(row).each(function(index){
    			var record = row[index];
    			var title = record.title;
    			var content = '<div style="padding:5px"><div class="fitem"><span class="view_customer11">分配人:'+record.assigner+"</span>";
    			content += '<span class="view_customer11">执行者:'+record.assignee+"</span>";
    			content += '<span class="view_customer11">截止日期:'+record.endDate+"</span></div>";
    			content +='<div class="fitem"><span class="view_customer11">任务状态:'+convertState(record.state)+"</span>";
    			content += '<span class="view_customer11">任务完成日期:</span></div>';
    			content += '<div class="fitem"><span class="view_customer12">详细信息 : <br><br>'+record.description+"</span></div></div>";
    			
    			$('#opportunityTask').accordion('add', {
    				title: title,
    				content: content,
    				selected: false
    			});
    		});
    		//$('#opportunityTask').accordion('remove',0);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	}
	viewOpportunityJs.viewCustomerComments = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var row = jQuery.parseJSON(result.payload.value);
    		$(row).each(function(index){
    			var record = row[index];
    			var title = record.user+' 于 '+record.time+' 评论';
    			var link = '<div style="text-align:center;padding:5px"><a href="#" class="easyui-linkbutton" onclick="javascript:viewOpportunityJs.deleteComment('
    						+record.id+',\''+title+'\')">删除</a></div>';
    			var content = '<p style="padding:10px;">'+record.content+'</p>'+link;
    			
    			$('#opportunityComment').accordion('add', {
    				title: title,
    				content: content,
    				selected: false
    			});
    		});
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	viewOpportunityJs.deleteComment = function(id,index){
		 $.get(
			  'dispatch/customer/opportunity/comments/delete',
			   {
			      id:id
			   },
			   function(result) 
			   {
					result = jQuery.parseJSON(result);
				    var res = result.result;
				    if(res == "SUCCESS")
				    {
				        $('#opportunityComment').accordion('remove', index);
				        	
				    }else if(result.result == "NOT_AUTHORIZED")
	                {
	                	$.messager.alert("Error","操作: 删除评论"+" 错误信息: 您没有权限 ");
	                }else if(result.result == "COMPONENT_ERROR")
	                {
	                	$.messager.alert("Error","操作: 删除评论"+" 错误信息: "+result.payload.value);
	                }else
	                {
	                	$.messager.alert("Error","操作: 删除评论"+" 错误信息: "+result.result);
	                }
			  });
	};
	viewOpportunityJs.openComment = function(){
		
		$('#addComment').dialog('open').dialog('setTitle','添加评论');
		$('#addComment').form('clear');
	};
	viewOpportunityJs.openTask = function(){
		taskActionJs.setRelate("opportunity");
		taskActionJs.setRelateID(opportunity.id);
		taskActionJs.setRelateName(opportunity.name);
		taskActionJs.setCallBack(addTaskComplete);
		taskActionJs.setUrl("dispatch/customer/opportunity/task/add")
		
		$('#addTask').dialog("open");
		$('#addTask').dialog('refresh','component/utility/createTask.html');
	};
	function addTaskComplete(result){
		$('#addTask').dialog("close");
		workbenchJs.loadData("dispatch/customer/opportunity/task/query?opp="+getOpportunityID(),viewOpportunityJs.loadTaskResult,"查询商业机会任务");
	}
	viewOpportunityJs.submitComment = function(){
		var url = "dispatch/customer/opportunity/comments/add";
		$('#addComment').dialog('close');
		
		$.post(
			url,
			{
				opp:getOpportunityID(),
				comment:$("textarea[name='add_comment']").val()
			},
			function (result)
			{
				result = jQuery.parseJSON(result);
		    	var res = result.result;
		        if(res == "SUCCESS")
		        {
		        	var record = jQuery.parseJSON(result.payload.value);
		        	$('#opportunityComment').accordion('add', {
						title: record.user+' 于 '+record.time+' 评论',
						content: '<p style="padding:10px;">'+record.content+'</p>',
						selected: false
					});
		        	
		        }else if(result.result == "NOT_AUTHORIZED")
                {
                	$.messager.alert("Error","操作: 添加评论"+" 错误信息: 您没有权限 ");
                }else if(result.result == "COMPONENT_ERROR")
                {
                	$.messager.alert("Error","操作: 添加评论"+" 错误信息: "+result.payload.value);
                }else
                {
                	$.messager.alert("Error","操作: 添加评论"+" 错误信息: "+result.result);
                }
			});

	};
	
	$('#consultList').datagrid({
		fitColumns:true,
		singleSelect:true,
		url:null,
		rownumbers:true,
	    columns:[[
					{title:'ID',field:'id',width:80},
	             	{title:'标题',field:'title',width:200,sortable:true},
	             	{title:'创建者',field:'addUser',width:80,sortable:true},
	             ]],
		pagination:false,
	    onLoadSuccess: function(){   
	        $('#consultList').datagrid('loaded');
	    },

	    onDblClickCell: openConsult
	});
	function convertState(state){
		if(state == 'processing')
			return '进行中';
		else if(state == 'complete')
			return '完成';
		else if(state == 'expired')
			return '已过期';
		else if(state == 'approving')
			return '正在批准';
		else if(state == 'reject')
			return '被拒绝';
	}
	function convertLevel(level){
		if(level = 'normal')
			return '普通';
		if(level = 'high')
			return '高';
	}
	
	function openConsult(){
        var row = $('#consultList').datagrid('getSelected');
        if (row){
    		$('#ConsultDetail').dialog("open");
    		$('#ConsultDetail').dialog('refresh','component/customer/viewConsult.jsp?id='+row.id);
        }else
        	{
        		$.messager.alert("Error","请选择一条记录");
        	}
	};
}( window.viewOpportunityJs = window.viewOpportunityJs || {}, jQuery ));

$(function(){
	viewOpportunityJs.init();
});
	</script>
	    <style type="text/css">
	            #fm,#findCustomerfm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
         .c-label{
            display:inline-block;
            width:150px;
            height:24px;
            padding:0;
            line-height:24px
        }
        .view_customer11{
        	margin-bottom:5px;
        	margin-left:9px;
        	display:inline-block;
            width:200px;
        }
        .view_customer12{
        	margin-bottom:5px;
        	margin-left:9px;
        	display:inline-block;
        }
        
    </style>